package cst.neu.yuejian_31.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.activity.TrainDetailActivity;
import cst.neu.yuejian_31.bean.TrainBean;

/**
 * Created by Alienware on 2017/10/26.
 */

public class TrainAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<TrainBean> list;

    public TrainAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<TrainBean>list){
        this.list=list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView Trainname,Trainintro;
        public ViewHolder(View itemView){
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.train_pic);
            Trainname= (TextView) itemView.findViewById(R.id.train_name);
//            Trainintro= (TextView) itemView.findViewById(R.id.train_intro)

        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item_train,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final TrainBean train =list.get(position);
        if(train==null)
            return;
       ViewHolder viewHolder= (ViewHolder) holder;
        Picasso.with(context).load(Constant.TRAIN_URL+train.getTrain_pic())
                .placeholder(R.drawable.julebu)
                .into(viewHolder.imageView);
        viewHolder.Trainname.setText(train.getTrain_name());
//        viewHolder.Trainintro.setText(train.getTrain_intro());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, TrainDetailActivity.class);
//                Bundle bundle=new Bundle();
//                bundle.putInt("activity_id",activity.getActivity_id());
                intent.putExtra("train_id",train.getTrain_id());
                intent.putExtra("train_name",train.getTrain_name());
                intent.putExtra("train_intro",train.getTrain_intro());
                intent.putExtra("train_pic",train.getTrain_pic());
                intent.putExtra("train_money",train.getTrain_price());;
                intent.putExtra("train_address",train.getTrain_address());

//                intent.putExtras(bundle)；
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list==null)return 0;
        else
            return list.size();
    }
}